package com.zz.parkingsystem.controller.user;

import cn.hutool.core.util.RandomUtil;
import com.zz.parkingsystem.entity.*;
import com.zz.parkingsystem.service.*;
import com.zz.parkingsystem.util.MD5Util;
import com.zz.parkingsystem.util.Result;
import com.zz.parkingsystem.util.ResultGenerator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.server.Session;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;


@Controller
@RequestMapping("/user")
@Api(value = "",tags = "用户接口")
public class UserController {
    @Autowired
    private UserService userService;
    //使用中
   //登录
    @ResponseBody
    @PostMapping (value = "/login")
    public Result login(@RequestBody User user, HttpSession session){
        User userByMysql = userService.selByTelAndPassWord(user);
        if(userByMysql==null){
            return ResultGenerator.genFailResult("电话或密码错误");
        }
        session.setAttribute("user",userByMysql);
        return ResultGenerator.genSuccessResult(userByMysql);
    }
    //使用中
    //注册
    @ResponseBody
    @PostMapping(value = "/add")
    @ApiOperation(value = "用户注册")
    public Result addUser(@Validated @RequestBody User user){
        User userNew = userService.addUser(user);
        if(userNew!=null){
            return ResultGenerator.genSuccessResult(userNew);
        }
        return ResultGenerator.genFailResult("注册失败，可能电话号码重复");
    }
    //修改用户
    @ResponseBody
    @PostMapping(value = "/update")
    @ApiOperation(value = "用户修改")
    public Result update(@Validated @RequestBody User user){
        userService.updateByPrimaryKeySelective(user);
        return ResultGenerator.genSuccessResult("修改成功");
    }
}
